package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //存放begin到end每一天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Double> turnoverList=new ArrayList<>();
        dateList.forEach(date->{
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            Map map=new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);
            //营业额为状态已经完成的状态的合计
            Double turnover = orderMapper.sumByMap(map);
            turnover=turnover==null?0.0:turnover;
            turnoverList.add(turnover);
            //selece sum(amount) from orders where stasta=5 and order_time>orderTime and order_time< orderTime
        });
        TurnoverReportVO res=new TurnoverReportVO();
        String date = StringUtils.join(dateList,",");
        String money=StringUtils.join(turnoverList,",");
        res.setDateList(date);
        res.setTurnoverList(money);
        return res;
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //存放begin到end每一天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> totalUserList=new ArrayList<>();
        List<Integer> newUserList=new ArrayList<>();
        dateList.forEach(date->{
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            //select count(id) from
            Map map=new HashMap();
            map.put("end",endTime);
            Integer totolUser = userMapper.countByMap(map);
            totalUserList.add(totolUser);

            map.put("begin",beginTime);
            Integer newUser = userMapper.countByMap(map);
            newUserList.add(newUser);
        });
        String totalUser = StringUtils.join(totalUserList,",");
        String newUser = StringUtils.join(newUserList, ",");
        String date = StringUtils.join(dateList, ",");
        return UserReportVO.builder()
                .dateList(date)
                .totalUserList(totalUser)
                .newUserList(newUser)
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //存放begin到end每一天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> sumOrderList=new ArrayList<>();
        List<Integer> completeOrderList=new ArrayList<>();
        dateList.forEach(date->{
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            Integer orderCount = getOrderCount(beginTime, endTime, null);
            Integer vaildOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
            sumOrderList.add(orderCount);
            completeOrderList.add(vaildOrderCount);
        });

        Integer sum = sumOrderList.stream().reduce(Integer::sum).get();
        Integer vaildSum = completeOrderList.stream().reduce(Integer::sum).get();
        String date = StringUtils.join(dateList, ",");
        String count = StringUtils.join(sumOrderList, ",");
        String vaild=StringUtils.join(completeOrderList,",");

        Double completionRate=0.0;
        if(sum!=0){
           completionRate = vaildSum.doubleValue()/sum;
        }

        return OrderReportVO.builder()
                .dateList(date)
                .orderCompletionRate(completionRate)
                .orderCountList(count)
                .validOrderCountList(vaild)
                .totalOrderCount(sum)
                .validOrderCount(vaildSum)
                .build();
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime=LocalDateTime.of(end,LocalTime.MAX);
        List<GoodsSalesDTO> saleTop10 = orderMapper.getSaleTop10(beginTime, endTime);

        List<String> nameList = saleTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numberList = saleTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());

        String names = StringUtils.join(nameList, ",");
        String numbers=StringUtils.join(numberList,",");
        return SalesTop10ReportVO.builder()
                .nameList(names)
                .numberList(numbers)
                .build();
    }

    private Integer getOrderCount(LocalDateTime beginTime,LocalDateTime endTime,Integer status){
        Map map=new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        map.put("status",status);
        Integer count = orderMapper.getCountByMap(map);
        return count;
    }
}
